package com.my.system.dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DataBaseConnection {
	private static Connection con = null;
	static {
		try {
			Class.forName(getProperties("db.driver"));
			con = DriverManager.getConnection(getProperties("db.url"),
					getProperties("db.username"), getProperties("db.password"));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		return con;
	}

	public static boolean close() {
		if (con != null) {
			try {
				con.close();
				return true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return false;
	}

	private static String getProperties(String key) {
		Properties pro = new Properties();
		try {
			pro.load(new FileInputStream("databasecon.properties"));
			String value = pro.getProperty(key);
			return value;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
